<?php

class announcements extends CourseController {

	protected $articlesModel = new article();

	function index() {
		$article = $articlesModel->getArticle( $name );
		$this->templateVars =  array(
			'title' =>  $this->org[0]['description'] .' - Announcements - FEELS ',
			'heading' => $this->org[0]['description'],
			'highlight' => 'organnouncements',
			'currentOrg' => $name,
			'content' => BufferedView::instance('org/announcements',
				array(
					'currentOrg' => $name,
					// 'name' => $org['name'],
					// 'description' => $org['description'],
					// 'long_description' => $org['long_description'],
					'announcements' => $article,
					// 'events' => $events,
					'isAdmin' => $this->iAmAdmin
				)
			)->getBuffer()
		);
	}

				function show() {
					$article = $articlesModel->getArticleById( $param );
					$this->templateVars =  array(
						'title' =>  $this->org[0]['description'] .' - View Announcement - FEELS ',
						'heading' => $this->org[0]['description'],
						'highlight' => 'organnouncements',
						'currentOrg' => $name,
						'content' => BufferedView::instance('org/announcement',
								array(
									'currentOrg' => $name,
									'title' => $article['title'],
									'date' => $article['date'],
									'post' => $article['post'],
									'id' => $article['article_id'],
									'isAdmin' => $this->iAmAdmin
								)
							)->getBuffer()
					);
					if( $this->iAmMember ) {
						$commentModel = new commentArticle();
						$comments = $commentModel->getComment($article['article_id']);
						$this->templateVars['content'] .= BufferedView::instance('org/comments',
								array(
									'comments'=>$comments,
									'isAdmin'=>$this->iAmAdmin,
									'orgname'=>$name,
									'section' => 'announcements'
								)
						)->getBuffer();
						$this->templateVars['content'] .= BufferedView::instance('org/forms/comment',
								array(
									'action' => "/org/view/$name/announcements/comment",
										'hiddenInputs' => array(
										'article_id' => $param
									)
								)
						)->getBuffer();
					}
					if( $param2 === 'blankcomment' ) {
						$this->templateVars['content'] .= 'Do not leave the comment field blank';
					}
				}


				function comment() {
					if( !empty( $_POST['post'] ) ) {
						$commentModel = new commentArticle();
						$commentModel->insertComment ( $_POST['article_id'], $_SESSION['stud_no'], gmdate('Y-m-d H:i:s'), $_POST['post'] );
						header("Location: /org/view/$name/announcements/show/" . $_POST['article_id'] 	);
					}
					else {
						header("Location: /org/view/$name/announcements/show/" . $_POST['article_id'] . "/blankcomment" );
					}
					$this->templateVars =  array(
						'title' =>  $this->org[0]['description'] .' - Comment - FEELS ',
						'heading' => $this->org[0]['description'],
						'highlight' => 'organnouncements',
						'currentOrg' => $name,
						'content' => 'Comment posted. Click here'
					);
				}
				function commentDelete() {
					$commentModel = new commentArticle();
					$commentModel->deleteComment( $param );
					header('Location: ' . $_SERVER['HTTP_REFERER'] );
					//header("Location: /org/view/$name/announcements/show/" . $_POST['article_id']);
				}
				else if( $action === 'add' ) {
					$orgModel = new organization();
					$orgId = $orgModel->getOrgId( $name );
					$this->templateVars =  array(
						'title' =>  $this->org[0]['description'] .' - Add Announcement - FEELS ',
						'heading' => $this->org[0]['description'],
						'highlight' => 'organnouncements',
						'currentOrg' => $name,
						'content' => BufferedView::instance('org/forms/announcement',
							array(
								'action'=>"/org/view/$name/announcements/add-confirm",
								'hiddenInputs'=>array('org_id'=>$orgId)
							)
						)->getBuffer()
					);
				}
				else if( $action === 'add-confirm' ) {
					if( !empty($_POST['title']) && !empty($_POST['post'] ) ) {
						try {
							$articlesModel->insertArticle(
								$_POST['org_id'],
								$_POST['title'],
								$_POST['post'],
								gmdate('Y-n-d H:i:s')
							);
							//$transport = Swift_SmtpTransport::newInstance();
							//$transport = Swift_SmtpTransport::newInstance('localhost', 25);
							$transport = Swift_SmtpTransport::newInstance('mail.steadfast.net', 25);
							$mailer = Swift_Mailer::newInstance($transport);
							$mailinglist = $this->memberModel->getMailAddresses($this->org[0]['org_id']);
							$message = Swift_Message::newInstance('An announcement has been posted to FEELS')
							  ->setFrom(array('admin@sooc.dijiyd.com' => 'FEELS'))
							  ->setBcc($mailinglist)
							  ->setBody("Hello,
You're receiving this message because an announcement has been posted to {$this->org[0]['description']} at FEELS:

{$_POST['title']}

{$_POST['post']}

Please visit {$this->org[0]['description']} at FEELS.");

							$result = $mailer->send($message);
							$this->templateVars =  array(
								'title' =>  $this->org[0]['description'] .' - Add Announcement - FEELS ',
								'heading' => $this->org[0]['description'],
								'highlight' => 'organnouncements',
								'currentOrg' => $name,
								'content' => 'Announcement has been posted and mailed to '. $result .' members. Click <a href="/org/view/' . $name . '/announcements">here</a> to go back to announcements.'
							);
						}
						catch( Exception $e ) {
							$this->templateVars =  array(
								'title' =>  $this->org[0]['description'] .' - Add Announcement - FEELS ',
								'heading' => $this->org[0]['description'],
								'highlight' => 'organnouncements',
								'currentOrg' => $name,
								'content' => 'There has been an error. ' . 	$e->getMessage()
							);
						}
					}
					else {
						$this->templateVars =  array(
							'title' =>  $this->org[0]['description'] .' - Add Announcement - FEELS ',
							'heading' => $this->org[0]['description'],
							'highlight' => 'organnouncements',
							'currentOrg' => $name,
							'content' => '<p>Please fill in all fields.</p>'
								. BufferedView::instance('org/forms/announcement',
								array(
									'action'=>"/org/view/$name/announcements/add-confirm",
									'title'=> $article['title'],
									'post'=> $article['post'],
									'hiddenInputs'=>array('org_id'=>$orgId)
								)
							)->getBuffer()
						);
					}
				}
				else if( $action === 'edit' ) {
					$article = $articlesModel->getArticleById( $param );
					$this->templateVars =  array(
						'title' =>  $this->org[0]['description'] .' - Edit Announcement - FEELS ',
						'heading' => $this->org[0]['description'],
						'highlight' => 'organnouncements',
						'currentOrg' => $name,
						'content' => BufferedView::instance('org/forms/announcement',
							array(
								'action'=>"/org/view/$name/announcements/edit-confirm",
								'title'=> $article['title'],
								'post'=> $article['post'],
								'hiddenInputs'=>array('article_id'=>$param)
							)
						)->getBuffer()
					);
				}
				else if( $action === 'edit-confirm' ) {
					if( !empty($_POST['title']) && !empty($_POST['post'] ) ) {
						try {
							$articlesModel->updateArticle(
								$_POST['article_id'],
								$_POST['title'],
								$_POST['post'],
								gmdate('Y-n-d H:i:s')
							);
							$this->templateVars =  array(
								'title' =>  $this->org[0]['description'] .' - Edit Announcement - FEELS ',
								'heading' => $this->org[0]['description'],
								'highlight' => 'organnouncements',
								'currentOrg' => $name,
								'content' => 'Announcement has been updated. Click <a href="/org/view/' . $name . '/announcements">here</a> to go back to announcements.'
							);
						}
						catch( Exception $e ) {
							$this->templateVars =  array(
								'title' =>  $this->org[0]['description'] .' - Edit Announcement - FEELS ',
								'heading' => $this->org[0]['description'],
								'highlight' => 'organnouncements',
								'currentOrg' => $name,
								'content' => 'There has been an error. ' . 	$e->getMessage()
							);
						}
					}
					else {
						$this->templateVars =  array(
							'title' =>  $this->org[0]['description'] .' - Edit Announcement - FEELS ',
							'heading' => $this->org[0]['description'],
							'highlight' => 'organnouncements',
							'currentOrg' => $name,
							'content' => '<p>Please fill in all fields</p>'
							. BufferedView::instance('org/forms/announcement',
								array(
									'action'=>"/org/view/$name/announcements/edit-confirm",
									'title'=> $_POST['title'],
									'post'=> $_POST['post'],
									'hiddenInputs'=>array('article_id'=>$_POST['article_id'])
								)
							)->getBuffer()
						);
					}
				}
				else if( $action === 'delete' ) {
					$article = $articlesModel->getArticleById( $param );
					$this->templateVars =  array(
						'title' =>  $this->org[0]['description'] .' - Delete Announcement - FEELS ',
						'heading' => $this->org[0]['description'],
						'highlight' => 'organnouncements',
						'currentOrg' => $name,
						'content' => 'Are you sure you wish to delete the following announcement?'
							. BufferedView::instance('org/announcement',
								array(
									'name' => $this->org['name'],
									'description' => $this->org['description'],
									'long_description' => $this->org['long_description'],
									'title' => $article['title'],
									'date' => $article['date'],
									'post' => $article['post'],
								)
							)->getBuffer()
							. BufferedView::instance('org/forms/confirm',
								array(
									'action' => "/org/view/$name/announcements/delete-confirm",
									'hiddenInputs' => array(
										'article_id' => $param
									)
								)
							)->getBuffer()
					);
				}
				else if( $action === 'delete-confirm' ) {
					try {
						if( $_POST['confirm-yes'] ) {
							$articlesModel->deleteArticle( $_POST['article_id'] );
							$this->templateVars =  array(
								'title' =>  $this->org[0]['description'] .' - Delete Announcement - FEELS ',
								'heading' => $this->org[0]['description'],
								'highlight' => 'organnouncements',
								'currentOrg' => $name,
								'content' => 'Announcement has been removed. Click <a href="/org/view/' . $name . '/announcements">here</a> to go back to announcements.'
							);
						}
						else {
							header("Location: /org/view/$name/announcements");
						}
					} catch( Exception $e ) {
						$this->templateVars =  array(
							'title' =>  $this->org[0]['description'] .' - Delete Announcement - FEELS ',
							'heading' => $this->org[0]['description'],
							'highlight' => 'organnouncements',
							'currentOrg' => $name,
							'content' => 'There has been an error. ' . 	$e->getMessage()
						);
					}
				}

	
}

